<template>
<div class="activity-list">
  <div style="display:flex;align-items: center;justify-content: space-between;margin:0 0 10px;">
    <div style="display:flex;align-items: center;">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="专栏名称">
          <el-input clearable size="small" v-model="formInline.name" placeholder="标题"></el-input>
        </el-form-item>
        <el-form-item label="报名状态">
          <el-select clearable size="small" v-model="formInline.enrollStatus" placeholder="请选择">
            <el-option
              v-for="item in [{
                value: '0',
                label: '未开始'
              },{
                value: '1',
                label: '进行中'
              },{
                value: '2',
                label: '已结束'
              }]"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-select clearable size="small" v-model="formInline.state" placeholder="请选择">
            <el-option
              v-for="item in [{
                value: '1',
                label: '启用'
              },{
                value: '0',
                label: '禁用'
              },]"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否首页展示">
          <el-select clearable size="small" v-model="formInline.isShow" placeholder="请选择是否首页展示">
            <el-option
              v-for="item in [{
                value: '1',
                label: '是'
              },{
                value: '0',
                label: '否'
              },]"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专栏分类">
          <el-cascader @change="changeType" @expand-change="expandChangeType" v-model="typeId" :options="options" size="small" :props="props"></el-cascader>
        </el-form-item>
      </el-form>
        <el-button size="small" type="primary" @click="onSubmit">搜索</el-button>
        <el-button size="small" @click="reset">重置</el-button>
      </div>
      <div>
        <el-button size="small" type="primary" @click="add">新建专栏</el-button>
      </div>
  </div>
  <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
    <el-table-column label="ID" width="150">
      <template slot-scope="scope">
        {{ scope.row.id }}
      </template>
    </el-table-column>
    <el-table-column label="专栏海报" width="180">
      <template slot-scope="scope">
        <img :src="scope.row.picUrl" alt="" style="width:80px;height:40px;">
      </template>
    </el-table-column>
    <el-table-column label="专栏名称" width="200">
      <template slot-scope="scope">
        {{ scope.row.name }}
      </template>
    </el-table-column>
    <el-table-column label="专栏分类" width="200">
      <template slot-scope="scope">
        {{ scope.row.courseTypeDetail.fullName }}
      </template>
    </el-table-column>
    <!-- <el-table-column label="专栏简介" width="200">
      <template slot-scope="scope">
        {{ scope.row.des }}
      </template>
    </el-table-column> -->
    <el-table-column label="费用" width="100">
      <template slot-scope="scope">
        {{ scope.row.fee?scope.row.fee:'免费' }}
      </template>
    </el-table-column>
    <el-table-column label="活动状态" width="100">
      <template slot-scope="scope">
        <el-tag style="cursor:pointer;" size="small" v-if="scope.row.enrollStatus == 0" type="danger">未开始</el-tag>
        <el-tag style="cursor:pointer;" size="small" v-else-if="scope.row.enrollStatus == 1" type="success">进行中</el-tag>
        <el-tag style="cursor:pointer;" size="small" v-else-if="scope.row.enrollStatus == 2" type="info">已结束</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="课程数量" width="100">
      <template slot-scope="scope">
        {{ scope.row.lessonVOList?scope.row.lessonVOList.length:0 }}
      </template>
    </el-table-column>
    <el-table-column label="试卷" width="300">
      <template slot-scope="scope">
        <div v-if="scope.row.exercisePaperDetail&&scope.row.exercisePaperDetail.paperName" style="padding: 2px 0;">
          练习试卷：<el-tag @click="goPaper(scope.row.exercisePaperDetail.id, '/paper/test', scope.row.exercisePaperDetail.paperName)" style="cursor:pointer;" size="small">{{ scope.row.exercisePaperDetail.paperName }}</el-tag>
        </div>
        <div v-if="scope.row.workPaperDetail&&scope.row.workPaperDetail.paperName" style="padding: 2px 0;">
          作业试卷：<el-tag @click="goPaper(scope.row.workPaperDetail.id, '/paper/task', scope.row.workPaperDetail.paperName)" style="cursor:pointer;" size="small">{{ scope.row.workPaperDetail.paperName }}</el-tag>
        </div>
        <div v-if="scope.row.examinationPaperDetail&&scope.row.examinationPaperDetail.paperName" style="padding: 2px 0;">
          考试试卷：<el-tag @click="goPaper(scope.row.examinationPaperDetail.id, '/paper/exam', scope.row.examinationPaperDetail.paperName)" style="cursor:pointer;" size="small">{{ scope.row.examinationPaperDetail.paperName }}</el-tag>
        </div>
      </template>
    </el-table-column>
    <!-- <el-table-column label="创建人" width="100">
      <template slot-scope="scope">
        {{ scope.row.creater }}
      </template>
    </el-table-column> -->
    <el-table-column label="创建时间" width="200">
      <template slot-scope="scope">
        {{ scope.row.createTime }}
      </template>
    </el-table-column>
    <el-table-column label="是否首页显示" width="150">
      <template slot-scope="scope">
        <el-tag @click="changeTop(scope.row.id, 0)" style="cursor:pointer;" size="small" v-if="scope.row.isShow == 1" type="success" >取消首页显示</el-tag>
        <el-tag @click="changeTop(scope.row.id, 1)" style="cursor:pointer;" size="small" v-else type="danger">设置首页显示</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="启用状态" width="100">
      <template slot-scope="scope">
        <el-tag style="cursor:pointer;" size="small" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
        <el-tag style="cursor:pointer;" size="small" v-else type="success">启用</el-tag>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="220">
      <template slot-scope="scope">
        <el-button @click="changeState(scope.row)" type="text" style="color:#ff0000;" v-if="scope.row.state == 1">禁用</el-button>
        <el-button @click="changeState(scope.row)" type="text" v-else>启用</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="openEnroll(scope.row)" type="text">学员名单</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="edit(scope.row)" type="text">编辑</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-popconfirm
          @onConfirm="del(scope.row)"
          confirm-button-text='确定'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="是否删除本条数据？">
          <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="margin:10px 0 0 0;text-align:right;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageObj.pageNum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="pageObj.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageObj.total">
    </el-pagination>
  </div>

  <el-drawer
    title="名单管理"
    direction="rtl"
    size="1100px"
    :visible.sync="enrollModal">
    <div style="padding:0 30px;">
      <div style="margin-bottom:20px;">
        <el-button @click="exportExcel" size="small" type="primary">导出Excel</el-button>
        <!-- <el-button @click="getEnrollList" size="small" type="primary">刷新</el-button> -->
      </div>
      <el-table ref="multipleTable" v-loading="enrollLoading" :data="enrollList" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
        <el-table-column label="专栏名称">
          <template slot-scope="scope">
            {{ scope.row.objName }}
          </template>
        </el-table-column>
        <el-table-column label="用户ID" width="100">
          <template slot-scope="scope">
            {{ scope.row.userDetail.id }}
          </template>
        </el-table-column>
        <el-table-column label="用户头像" width="80">
          <template slot-scope="scope">
            <img :src="scope.row.userDetail.avatar" alt="" style="width:40px;height:40px">
          </template>
        </el-table-column>
        <el-table-column label="用户昵称" width="100">
          <template slot-scope="scope">
            {{ scope.row.userDetail.nickname }}
          </template>
        </el-table-column>
        <el-table-column label="用户手机号" width="150">
          <template slot-scope="scope">
            {{ scope.row.userDetail.mobile }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="用户IP" width="150">
          <template slot-scope="scope">
            {{ scope.row.userDetail.ip }}
          </template>
        </el-table-column> -->
        <el-table-column label="专栏费用" width="100">
          <template slot-scope="scope">
            {{ scope.row.fee?scope.row.fee:'免费' }}
          </template>
        </el-table-column>
        <el-table-column label="报名时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
      </el-table>

      <div style="margin:10px 0 0 0;text-align:right;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="enrollPageObj.pageNum"
          :page-sizes="[10]"
          :page-size="enrollPageObj.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="enrollPageObj.total">
        </el-pagination>
      </div>
    </div>
  </el-drawer>

</div>
</template>

<script>
import {
  getListApi,
  changeStateApi,
  deleteApi,
  gettypeListApi,
  getTypeListById,
  saveApi
} from '@/api/column'

import {getEnrollListApi} from '@/api/activity'

export default {

  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    let _this = this
    return {
      typeId: [],
      props: {
        value: 'id',
        label: 'name',
        lazy: true,
        async lazyLoad (node, resolve) {
          if (node.data) {
            setTimeout(() => {
              _this.getChildArea(node, resolve)//懒加载后几层数据
            }, 1000);
          }
        }
      },
      options: [],
      list: [],
      listLoading: false,
      enrollLoading: false,
      currentPage: 1,
      formInline: {
        name: '',
        state: '',
        isShow: '',
        enrollStatus: '',
      },
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      enrollModal: false,
      enrollList: [],
      enrollPageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      enrollObj: {},
      searchTypeId: ''
    }
  },
  created() {
    this.fetchData()
    this.getTypeList()
  },
  methods: {
    getEnrollList() {
      this.enrollLoading = true
      getEnrollListApi({
        objId: this.enrollObj.id,
        type: 1
      }).then((res) => {
        this.enrollLoading = false
        this.enrollModal = true
        this.enrollList = res.data.result
        this.enrollPageObj.total = res.data.total
      })
    },
    goPaper(id, path, name) {
      this.$router.push({
        path: path,
        query: {
          id: id,
          name: name,
        }
      })
    },
    changeTop(id, isShow) {
      saveApi({
        id: id,
        isShow: isShow
      }).then((res) => {
        this.fetchData()
      })
    },
    expandChangeType(e) {
      this.searchTypeId = e[e.length-1]
    },
    changeType(e) {
      console.log(e)
    },
    getChildArea(node, resolve) {
      let childProjects = []
      getTypeListById(node.value).then((res) => {
        childProjects = res.data
        if (node.level > 1) { // level从0开始，统一设置第3层为叶子节点，再没有下一层了
          for (const item of childProjects) {
            item.leaf = true
          }
        }
        resolve(childProjects)// 获取数据后调用resolve将子项数据渲染至视图
      })
    },
    getTypeList() {
      gettypeListApi({
        pageNum: 1,
        pageSize: 1000,
      }).then((res) => {
        this.options = res.data.result
      })
    },
    exportExcel() {
      window.open(`https://service.visualbook.cn/enroll/user/export?pageNum=1&type=1&pageSize=${this.enrollPageObj.total}`)
    },
    openEnroll(row) {
      this.enrollObj = row
      this.getEnrollList()
    },
    onSubmit() {
      this.fetchData()
    },
    del(row) {
      deleteApi({
        ids: row.id
      }).then((res) => {
        this.fetchData()
      })
    },
    reset() {
      this.pageObj.pageSize = 10
      this.pageObj.pageNum = 1
      this.formInline.name = ''
      this.formInline.state = ''
      this.formInline.isShow = ''
      this.formInline.enrollStatus = ''
      this.typeId = []
      this.fetchData()
    },
    add() {
      this.$router.push({
        path: '/column/add'
      })
    },
    edit(row) {
      this.$router.push({
        path: '/column/edit',
        query: {
          editId: row.id
        }
      })
    },
    changeState(row) {
      changeStateApi({
        id: row.id,
        state: row.state == 1? 0: 1,
      }).then((res) => {
        this.$message.success('操作成功！');
        this.fetchData()
      })
    },
    fetchData() {
      this.listLoading = true
      getListApi({
        name: this.formInline.name,
        state: this.formInline.state,
        isShow: this.formInline.isShow,
        enrollStatus: this.formInline.enrollStatus,
        courseTypeId: this.typeId.length?this.typeId[this.typeId.length - 1]:'',
        pageSize: this.pageObj.pageSize,
        pageNum: this.pageObj.pageNum,
      }).then(response => {
        this.list = response.data.result
        this.pageObj.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageObj.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style lang="scss" scoped>
   .activity-list{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
